using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace CuthluralSwitcher
{
    class ImageManager
    {
        public static Image AddImageInformationText(Image img, CFileInfo info)
        {
            Brush FontColor = Brushes.White;
            Bitmap myBitmap = new Bitmap(img);
            Color pixCol = myBitmap.GetPixel(8, 8);
            float brightness = pixCol.GetBrightness();
            if (brightness > 0.5)
            {
                FontColor = Brushes.Black;
            }

            Graphics g = Graphics.FromImage(myBitmap);

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            if (info.FileSubject.Trim() == string.Empty && info.FileTitle == string.Empty)
            {
                g.DrawString(info.FileAuthor, new Font("Georgia", 10, FontStyle.Italic), FontColor, new PointF(5, 5));
            }
            else if (info.FileSubject.Trim() != string.Empty)
            {
                g.DrawString(info.FileTitle + " (" + info.FileSubject + ")\n" + info.FileAuthor, new Font("Georgia", 10, FontStyle.Italic), FontColor, new PointF(5, 5));
            }
            else
            {
                g.DrawString(info.FileTitle + "\n" + info.FileAuthor, new Font("Georgia", 10, FontStyle.Italic), FontColor, new PointF(5, 5));
            }
            return myBitmap;
        }

        public static Image ResizeImage(Image FullsizeImage, int NewWidth, int MaxHeight, int MinHeight, bool OnlyResizeIfWider)
        {
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            int NewHeight = FullsizeImage.Height;
            if (FullsizeImage.Width < NewWidth)
            {
                float ratio = ((float)NewWidth / (float)FullsizeImage.Width);
                NewHeight = (int)Math.Floor((float)FullsizeImage.Height * ratio);
                if (NewHeight < MinHeight)
                {
                    NewHeight = MinHeight;
                    ratio = ((float)NewHeight / (float)FullsizeImage.Height);
                    NewWidth = (int)Math.Floor((float)FullsizeImage.Width * ratio);
                }
            }
            else if (FullsizeImage.Height < MinHeight)
            {
                NewHeight = MinHeight;
                float ratio = ((float)NewHeight / (float)FullsizeImage.Height);
                int ilWidth = NewWidth;
                NewWidth = (int)Math.Floor((float)FullsizeImage.Width * ratio);
                if (NewWidth < ilWidth)
                {
                    ratio = ((float)NewWidth / (float)FullsizeImage.Width);
                    NewHeight = (int)Math.Floor((float)FullsizeImage.Height * ratio);
                    if (NewHeight < MinHeight)
                    {
                        NewHeight = MinHeight;
                        ratio = ((float)NewHeight / (float)FullsizeImage.Height);
                        NewWidth = (int)Math.Floor((float)FullsizeImage.Width * ratio);
                    }
                }
            }
            else if (FullsizeImage.Width > NewWidth)
            {
                float ratio = ((float)NewWidth / (float)FullsizeImage.Width);
                NewHeight = (int)Math.Floor((float)FullsizeImage.Height * ratio);
                if (NewHeight < MinHeight)
                {
                    NewHeight = MinHeight;
                    ratio = ((float)NewHeight / (float)FullsizeImage.Height);
                    NewWidth = (int)Math.Floor((float)FullsizeImage.Width * ratio);
                }
            }

            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            FullsizeImage.Dispose();

            return NewImage;

        }
    }
}
